import facecat from "facecat"
import { invalidateView, isViewEnabled } from "./facecat";
/*
* 设置属性
* view:视图
* node:xml节点
*/
let setAttributeWeChat = function (view, node) {
	if (view.paint) {
		if (view.paint.defaultUIStyle == "dark") {
			view.backColor = "rgb(0,0,0)";
			view.borderColor = "rgb(100,100,100)";
			view.textColor = "rgb(255,255,255)";
			view.scrollBarColor = "rgb(100,100,100)";
		} else if (view.paint.defaultUIStyle == "light") {
			view.backColor = "rgb(255,255,255)";
			view.borderColor = "rgb(150,150,150)";
			view.textColor = "rgb(0,0,0)";
			view.scrollBarColor = "rgb(200,200,200)";
		}
	}
	if (!view.location) {
		view.location = new facecat.FCPoint(0, 0);
	}
	if (!view.size) {
		view.size = new facecat.FCSize(100, 30);
	}
	for (let i = 0; i < node.attributes.length; i++) {
		let name = node.attributes[i].nodeName.toLowerCase();
		let value = node.attributes[i].nodeValue;
		if (name && value) {
			if (name == "location") {
				let xStr = value.split(',')[0];
				let yStr = value.split(',')[1];
				if (xStr.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("leftstr", xStr);
				}
				else {
					view.location.x = parseInt(xStr);
				}
				if (yStr.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("topstr", yStr);
				}
				else {
					view.location.y = parseInt(yStr);
				}
			}
			else if (name == "size") {
				let xStr = value.split(',')[0];
				let yStr = value.split(',')[1];
				if (xStr.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("widthstr", xStr);
				}
				else {
					view.size.cx = parseInt(xStr);
				}
				if (yStr.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("heightstr", yStr);
				}
				else {
					view.size.cy = parseInt(yStr);
				}
			} else if (name == "text") {
				view.text = value;
			} else if (name == "backcolor") {
				let lowerStr = value.toLowerCase();
				if (lowerStr.indexOf("rgb") == 0) {
					view.backColor = value;
				} else {
					view.backColor = null;
				}
			} else if (name == "bordercolor") {
				let lowerStr = value.toLowerCase();
				if (lowerStr.indexOf("rgb") == 0) {
					view.borderColor = value;
				} else {
					view.borderColor = null;
				}
			} else if (name == "textcolor") {
				let lowerStr = value.toLowerCase();
				if (lowerStr.indexOf("rgb") == 0) {
					view.textColor = value;
				} else {
					view.textColor = null;
				}
			} else if (name == "layoutstyle") {
				view.layoutStyle = value.toLowerCase();
			} else if (name == "align") {
				view.align = value.toLowerCase();
			} else if (name == "vertical-align") {
				view.verticalAlign = value.toLowerCase();
			} else if (name == "dock") {
				view.dock = value.toLowerCase();
			} else if (name == "font") {
				view.font = value;
			} else if (name == "headerheight") {
				view.headerHeight = parseFloat(value);
			} else if (name == "cornerradius") {
				view.cornerRadius = parseFloat(value);
			} else if (name == "borderwidth") {
				view.borderWidth = parseFloat(value);
			} else if (name == "splitmode") {
				view.splitMode = value.toLowerCase();
			}else if (name == "autowrap") {
				view.autoWrap = value.toLowerCase() == "true";
			} else if (name == "name") {
                view.viewName = value;
			} else if (name == "tabindex") {
				view.tabIndex = parseInt(value);
			}else if (name == "tabstop") {
				view.tabStop = value.toLowerCase() == "true";
			}else if (name == "showvscrollbar") {
				view.showVScrollBar = value.toLowerCase() == "true";
				view.showHScrol = value.toLowerCase() == "true";
			} else if (name == "showhscrollbar") {
				view.showHScrollBar = value.toLowerCase() == "true";
			} else if (name == "visible") {
				view.visible = value.toLowerCase() == "true";
			} else if (name == "displayoffset") {
				view.visible = value.toLowerCase() == "true";
			} else if (name == "checked") {
				view.checked = value.toLowerCase() == "true";
			} else if (name == "buttonsize") {
				view.buttonSize = new facecat.FCSize(parseInt(value.split(',')[0]), parseInt(value.split(',')[1]));
			} else if (name == "topmost") {
				view.topMost = value.toLowerCase() == "true";
			} else if (name == "selectedindex") {
				view.selectedIndex = parseInt(value);
			} else if (name == "src") {
				view.src = value;
			} else if (name == "backimage") {
				view.backImage = value;
			} else if (name == "groupname") {
				view.groupName = value;
			} else if (name == "allowdragscroll") {
				view.allowDragScroll = value.toLowerCase() == "true";
			} else if (name == "allowpreviewsevent") {
				view.allowPreviewsEvent = value.toLowerCase() == "true";
			} else if (name == "allowdrag") {
				view.allowDrag = value.toLowerCase() == "true";
			} else if (name == "allowresize") {
				view.allowResize = value.toLowerCase() == "true";
			} else if (name == "indent") {
				view.indent = parseFloat(value);
			} else if (name == "showcheckbox") {
				view.showCheckBox = value.toLowerCase() == "true";
			} else if (name == "padding") {
				view.padding = new facecat.FCPadding(parseInt(value.split(',')[0]), parseInt(value.split(',')[1]), parseInt(value.split(',')[2]), parseInt(value.split(',')[3]));
			} else if (name == "margin") {
				view.margin = new facecat.FCPadding(parseInt(value.split(',')[0]), parseInt(value.split(',')[1]), parseInt(value.split(',')[2]), parseInt(value.split(',')[3]));
			} else if (name == "hoveredcolor") {
				let lowerStr = value.toLowerCase();
				if (lowerStr.indexOf("rgb") == 0) {
					view.hoveredColor = value;
				} else {
					view.hoveredColor = null;
				}
			} else if (name == "pushedcolor") {
				let lowerStr = value.toLowerCase();
				if (lowerStr.indexOf("rgb") == 0) {
					view.pushedColor = value;
				} else {
					view.pushedColor = null;
				}
			} else if (name == "layout") {
				view.layout = value;
			} else if (name == "width") {
				if (value.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("widthstr", value);
				}
				else {
					view.size.cx = parseInt(value);
				}
			}
			else if (name == "height") {
				if (value.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("heightstr", value);
				}
				else {
					view.size.cy = parseInt(value);
				}
			}
			else if (name == "top") {
				if (value.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("topstr", value);
				}
				else {
					view.location.y = parseInt(value);
				}
			}
			else if (name == "left") {
				if (value.indexOf("%") != -1) {
					if (!view.exAttributes) {
						view.exAttributes = new Map();
					}
					view.exAttributes.set("leftstr", value);
				}
				else {
					view.location.x = parseInt(value);
				}
			}else{
				view.exAttributes.set(name, value);
			}
		}
	}
};

/*
* 读取Xml中的树节点
* tree 树
* parentNode 父节点
* xmlNode Xml节点
*/
let readTreeXmlNodeWeChat = function (tree, parntNode, xmlNode) {
	let subNodes = xmlNode.childNodes;
	let treeNode = new facecat.FCTreeNode();
	treeNode.value = xmlNode.getAttribute("text");
	facecat.appendTreeNode(tree, treeNode, parntNode);
	for (let i = 0; i < subNodes.length; i++) {
		let child = subNodes[i];
		if (child.nodeType == 1) {
			let nodeName = child.nodeName.toLowerCase();
			if (nodeName == "node") {
				readTreeXmlNodeWeChat(tree, treeNode, child);
			}
		}
	}
};

/*
* 读取Xml
* paint 绘图对象
* node节点
* parent 父视图
*/
let readXmlNodeWeChat = function (paint, node, parent) {
	let subNodes = node.childNodes;
	for (let i = 0; i < subNodes.length; i++) {
		let child = subNodes[i];
		if (child.nodeType == 1) {
			let view = null;
			let type = "";
			let nodeName = child.nodeName.toLowerCase();
			if (nodeName == "div" || nodeName == "view") {
				type = child.getAttribute("type");
				if (type == "splitlayout") {
					view = new facecat.FCSplitLayoutDiv();
				}
				else if (type == "layout") {
					view = new facecat.FCLayoutDiv();
				}
				else if (type == "tab") {
					view = new facecat.FCTabView();
				}
				else if (type == "tabpage") {
					view = new facecat.FCTabPage();
				}
				else if (type == "radio") {
					view = new facecat.FCRadioButton();
				}
				else if (type == "text") {
					view = new facecat.FCTextBox();
				}
				else if (type == "checkbox") {
					view = new facecat.FCCheckBox();
				} else if (type == "range") {
					view = new facecat.FCTextBox();
				} else if (type == "custom") {
					let cid = child.getAttribute("cid");
					view = new facecat.FCDiv();
					view.viewType = cid;
				}
				else {
					view = new facecat.FCDiv();
				}
			}
			else if (nodeName == "select") {
				view = new facecat.FCComboBox();
			}
			else if (nodeName == "input") {
				type = child.getAttribute("type");
				if (type == "radio") {
					view = new facecat.FCRadioButton();
				}
				else if (type == "text") {
					view = new facecat.FCTextBox();
				}
				else if (type == "checkbox") {
					view = new facecat.FCCheckBox();
				} else if (type == "range") {
					view = new facecat.FCTextBox();
				} else if (type == "custom") {
					let cid = child.getAttribute("cid");
					view = new facecat.FCView();
					view.viewType = cid;
				} else {
					view = new facecat.FCButton();
				}
			}
			else if (nodeName == "chart") {
				view = new facecat.FCChart();
			}
			else if (nodeName == "img") {
				view = new facecat.FCImage();
			}
			else if (nodeName == "calendar") {
				view = new facecat.FCCalendar();
			}
			else if (nodeName == "table") {
				view = new facecat.FCGrid();
			} else if (nodeName == "tree") {
				view = new facecat.FCTree();
			} else if (nodeName == "label") {
				view = new facecat.FCLabel();
			}
			else {
				view = new facecat.FCView();
			}
			view.views = new Array();
			view.paint = paint;
            view.parent = parent;
            setAttributeWeChat(view, child);          
			if (nodeName == "calendar") {
				facecat.initCalendar(view);
				view.selectedDay = facecat.getYear(view.years, 2022).months.get(10).days.get(1);
				facecat.updateCalendar(view);
			}
			if (view) {
				if (type == "tabpage") {
					let tabButton = new facecat.FCView();
                    tabButton.viewType = "tabbutton";
					let atrHeaderSize = child.getAttribute("headersize");
					if (atrHeaderSize) {
						tabButton.size = new facecat.FCSize(parseInt(atrHeaderSize.split(',')[0]), parseInt(atrHeaderSize.split(',')[1]));
					} else {
						tabButton.size = new facecat.FCSize(100, 20);
					}
					if (view.paint.defaultUIStyle == "dark") {
						tabButton.backColor = "rgb(0,0,0)";
						tabButton.borderColor = "rgb(100,100,100)";
						tabButton.textColor = "rgb(255,255,255)";
					} else if (view.paint.defaultUIStyle == "light") {
						tabButton.backColor = "rgb(255,255,255)";
						tabButton.borderColor = "rgb(150,150,150)";
						tabButton.textColor = "rgb(0,0,0)";
					}
					tabButton.text = view.text;
					facecat.addTabPage(view.parent, view, tabButton);
				} else {
					if (parent) {
						if (!parent.views) {
							parent.views = new Array();
						}
						parent.views.push(view);
					} else {
						paint.views.push(view);
					}
				}
				if (type == "splitlayout") {
					let atrDatum = child.getAttribute("datumsize");
					if (atrDatum) {
						view.size = new facecat.FCSize(parseInt(atrDatum.split(',')[0]), parseInt(atrDatum.split(',')[1]));
					}
					let splitter = new facecat.FCView();
					splitter.parent = view;
					if (view.paint.defaultUIStyle == "dark") {
						splitter.backColor = "rgb(100,100,100)";
					} else if (view.paint.defaultUIStyle == "light") {
						splitter.backColor = "rgb(150,150,150)";
					}
					let canDragSplitter = child.getAttribute("candragsplitter");
					if (canDragSplitter) {
						if (canDragSplitter == "true") {
							splitter.allowDrag = true;
						}
					}
					view.views.push(splitter);
                    view.splitter = splitter;
					let splitterposition = child.getAttribute("splitterposition");
					let splitStr = splitterposition.split(',');
					if (splitStr.length >= 4) {
						let splitRect = new facecat.FCRect(parseFloat(splitStr[0]), parseFloat(splitStr[1]), parseFloat(splitStr[2]), parseFloat(splitStr[3]));
						splitter.location = new facecat.FCPoint(splitRect.left, splitRect.top);
						splitter.size = new facecat.FCSize(splitRect.right - splitRect.left, splitRect.bottom - splitRect.top);
					} else {
						let sSize = parseFloat(splitStr[1]);
						let sPosition = parseFloat(splitStr[0]);
						if (view.layoutStyle == "lefttoright" || view.layoutStyle == "righttoleft") {
							splitter.location = new facecat.FCPoint(sPosition, 0);
							splitter.size = new facecat.FCSize(sSize, view.size.cy);
						} else {
							splitter.location = new facecat.FCPoint(0, sPosition);
							splitter.size = new facecat.FCSize(view.size.cx, sSize);
						}
					}
					readXmlNodeWeChat(paint, child, view);
					view.firstView = view.views[1];
					view.secondView = view.views[2];
					view.oldSize = view.size;
					facecat.resetSplitLayoutDiv(view);
				} else if (type == "tab") {
                    readXmlNodeWeChat(paint, child, view);
                    if (view.tabPages.length > 0) {
						let strSelectedIndex = child.getAttribute("selectedindex");
						if (strSelectedIndex) {
							let selectedIndex = parseInt(strSelectedIndex);
							if (selectedIndex >= 0 && selectedIndex < view.tabPages.length) {
								view.tabPages[selectedIndex].visible = true;
							} else {
								view.tabPages[view.tabPages.length - 1].visible = true;
							}
						} else {
							view.tabPages[view.tabPages.length - 1].visible = true;
						}
					}
				} else if (nodeName == "table") {
					let tSubNodes = child.childNodes;
					for (let n = 0; n < tSubNodes.length; n++) {
						let tChild = tSubNodes[n];
						if (tChild.nodeType == 1) {
							if (tChild.nodeName == "tr") {
                                let tsunNodes = tChild.childNodes;
                                let gridRow = null;
								for (let j = 0; j < tsunNodes.length; j++) {
									let sunNodeName = tsunNodes[j].nodeName.toLowerCase();
									if (sunNodeName.toLowerCase() == "th") {
										let gridColumn = new facecat.FCGridColumn();
										if (view.paint.defaultUIStyle == "light") {
											gridColumn.backColor = "rgb(230,230,230)";
											gridColumn.borderColor = "rgb(150,150,150)";
											gridColumn.textColor = "rgb(0,0,0)";
										} else if (view.paint.defaultUIStyle == "dark") {
											gridColumn.backColor = "rgb(50,50,50)";
											gridColumn.borderColor = "rgb(100,100,100)";
											gridColumn.textColor = "rgb(255,255,255)";
										}
										gridColumn.width = 100;
										let widthAttr = tsunNodes[j].getAttribute("width");
										if (widthAttr) {
											if (widthAttr.indexOf("%") != -1) {
												gridColumn.widthStr = widthAttr;
											}
											else {
												gridColumn.width = parseFloat(widthAttr);
											}
										}
										let backColorAttr = tsunNodes[j].getAttribute("backcolor");
										if (backColorAttr) {
											gridColumn.backColor = backColorAttr;
										}
										let textColorAttr = tsunNodes[j].getAttribute("textcolor");
										if (textColorAttr) {
											gridColumn.textColor = textColorAttr;
										}
										let borderColorAttr = tsunNodes[j].getAttribute("bordercolor");
										if (borderColorAttr) {
											gridColumn.borderColor = borderColorAttr;
										}
										let fontAttr = tsunNodes[j].getAttribute("font");
										if (fontAttr) {
											gridColumn.font = fontAttr;
										}
										gridColumn.text = tsunNodes[j].getAttribute("text");
										view.columns.push(gridColumn);
									} else if (sunNodeName.toLowerCase() == "td") {
										if (!gridRow) {
											gridRow = new facecat.FCGridRow();
											view.rows.push(gridRow);
										} 
										let gridCell = new facecat.FCGridCell();
                                        gridCell.value = tsunNodes[j].childNodes[0].nodeValue;
										gridRow.cells.push(gridCell);
									}
								}
							}
						}
					}
				}else if (nodeName == "tree") {
					let treeColumn = new facecat.FCTreeColumn();
					view.columns.push(treeColumn);
					let tSubNodes = child.childNodes;
					let columnWidth = 0;
					for (let n = 0; n < tSubNodes.length; n++) {
						let tChild = tSubNodes[n];
						if (tChild.nodeType == 1) {
							if (tChild.nodeName == "nodes") {
								let gridRow = null;
								let tsunNodes = tChild.childNodes;
								for (let j = 0; j < tsunNodes.length; j++) {
									let sunNodeName = tsunNodes[j].nodeName.toLowerCase();
									if (sunNodeName.toLowerCase() == "node") {
										readTreeXmlNodeWeChat(view, null, tsunNodes[j]);
									}
								}
							} else if (tChild.nodeName == "tr") {
								let tsunNodes = tChild.childNodes;
								for (let j = 0; j < tsunNodes.length; j++) {
									let sunNodeName = tsunNodes[j].nodeName.toLowerCase();
									if (sunNodeName.toLowerCase() == "th") {
										let widthAttr = tsunNodes[j].getAttribute("width");
										if (widthAttr) {
											if (widthAttr.indexOf("%") != -1) {
												treeColumn.widthStr = widthAttr;
											}
											else {
												columnWidth = parseFloat(widthAttr);
											}
										}
									}
								}
							}
						}
					}
					if (columnWidth > 0) {
						treeColumn.width = columnWidth;
					}
				}
				else if (nodeName == "calendar") {
					facecat.initCalendar(view);
					view.selectedDay = facecat.getYear(view.years, 2022).months.get(10).days.get(1);
					facecat.updateCalendar(view);
				}else if (nodeName == "select") {
					view.dropDownMenu = new facecat.FCMenu();
					view.dropDownMenu.comboBox = view;
					facecat.addView(view.dropDownMenu, paint);
					view.dropDownMenu.size.cx = view.size.cx;
					let tSubNodes = child.childNodes;
					for (let n = 0; n < tSubNodes.length; n++) {
						let tChild = tSubNodes[n];
						if (tChild.nodeType == 1) {
							if (tChild.nodeName == "option") {
								let menuItem = new facecat.FCMenuItem();
								facecat.addMenuItem(menuItem, view.dropDownMenu);
								setAttributeWeChat(menuItem, tChild);
							}
						}
					}
					if (view.dropDownMenu.items.length > 0) {
						let strSelectedIndex = child.getAttribute("selectedindex");
						if (strSelectedIndex) {
							let selectedIndex = parseInt(strSelectedIndex);
							if (selectedIndex >= 0 && selectedIndex < view.dropDownMenu.items.length) {
								view.selectedIndex = selectedIndex;
								view.text = view.dropDownMenu.items[selectedIndex].text;
							} else {
								view.selectedIndex = 0;
								view.text = view.dropDownMenu.items[0].text;
							}
						} else {
							view.selectedIndex = 0;
							view.text = view.dropDownMenu.items[0].text;
						}
					}
				}
				else {
                    if (view.viewType != "chart") {
                        readXmlNodeWeChat(paint, child, view);
                    }
				}
			}
		}
	}
};	

var Parser = require('../../lib/dom-parser')
/*
* 加载FaceCat
*/
let renderFaceCatWeChat = function (paint, xml, app) {
    paint.app = app;
    let XMLParser = new Parser.DOMParser();
    let xmlDoc = XMLParser.parseFromString(xml);
	for(let i = 0; i < xmlDoc.childNodes.length; i++){
        let rNode = xmlDoc.childNodes[i];
        if(rNode.nodeType == 1){
            for(let j = 0; j < rNode.childNodes.length; j++){
                let bNode = rNode.childNodes[j];
                if(bNode.nodeType == 1){
                    if(bNode.nodeName == "body"){
                        readXmlNodeWeChat(paint, bNode, null);
                        break;
                    }
                }
            }
        }
	}
	if (paint.onUpdateView) {
		paint.onUpdateView(paint.views);
	} else {
		facecat.updateViewDefault(paint.views);
	}
	facecat.invalidate(paint);
};

/*
* 显示文本框
*/
let showTextBox = function(view, lastView){
    let paint = view.paint;
    if(paint.editingTextBox){
        if(lastView && lastView != view && lastView.viewType == "textbox"){
            //lastView.text = paint.app.data.facecatContent;
            invalidateView(lastView);
            paint.editingTextBox = null;
        paint.app.setData({
            facecatStyle: "position:absolute;left:0px;top:0px;width:0px;height:0px;display:none;", 
            facecatFocus:false,
            facecatContent:""
         })
            return;
        }
    }
    paint.editingTextBox = view;
    let textColor = facecat.convertColor(view.textColor);
    let backColor = facecat.convertColor(view.backColor);
    let font = view.font;
    let strs = font.split(',');
    let fontFamily = strs[0];
    if (fontFamily == "Default") {
        fontFamily = paint.systemFont;
    }
    let fontSize = parseInt(strs[1]);
    if (paint.scaleFactorX != 1 || paint.scaleFactorY != 1) {
        fontSize = parseInt(Math.min(paint.scaleFactorX, paint.scaleFactorY) * parseFloat(strs[1]));
    }
    paint.app.setData({
        facecatStyle: "position:absolute;left:" + (facecat.clientX(view) + 1) * paint.scaleFactorX + "px;top:" + (facecat.clientY(view) + 1) * paint.scaleFactorY + "px;width:" + (view.size.cx - 2) * paint.scaleFactorX + "px;height:" + (view.size.cy - 2) * paint.scaleFactorY + "px;color:" + textColor + ";background-color:" + backColor + ";display:block;font-family:" + fontFamily + ";font-size:" + fontSize + "px;", 
        facecatContent:view.text,
        facecatFocus:true
     });
};

/*
* 隐藏文本框
*/
let hideTextBox = function(view){
    let paint = view.paint;
    if(paint.editingTextBox){
        paint.editingTextBox = null;
        paint.app.setData({
            facecatStyle: "position:absolute;left:0px;top:0px;width:0px;height:0px;display:none;", 
            facecatFocus:false,
            facecatContent:""
         })
    }
};

/*
* 触摸开始
*/
let handleTouchStart =  function(paint, e, callBack){
    if (paint.isMobile) {
        paint.cancelClick = false;
        let mp = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
        mp.x /= paint.scaleFactorX;
        mp.y /= paint.scaleFactorY;
        paint.touchDownPoint = new facecat.FCPoint(mp.x, mp.y);
        var touchDownView = facecat.findView(mp, paint.views);
        paint.touchDownView = touchDownView;
		let newClickTime = new Date().getTime();
		let clicks = 1;
		if(newClickTime - paint.lastClickTime < 250){
			paint.isDoubleClick = true;
			paint.lastClickTime = 0;
			clicks = 2;
		}else{
			paint.isDoubleClick = false;
		}
        facecat.checkShowMenu(paint);
        if (touchDownView && isViewEnabled(touchDownView)) {
            let lastFocusedView = paint.focusedView;
            paint.focusedView = touchDownView;
            let clx = facecat.clientX(paint.touchDownView);
            let cly = facecat.clientY(paint.touchDownView);
            paint.firstTouch = false;
            paint.secondTouch = false;
            paint.touchFirstPoint = new facecat.FCPoint();
            paint.touchSecondPoint = new facecat.FCPoint();
            let firstTouch = false, secondTouch = false;
            let firstPoint = new facecat.FCPoint(0, 0);
            let secondPoint = new facecat.FCPoint(0, 0);
            if (e.changedTouches.length >= 1) {
                firstTouch = true;
                firstPoint = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
                firstPoint.x /= paint.scaleFactorX;
                firstPoint.y /= paint.scaleFactorY;
                firstPoint.x -= clx;
                firstPoint.y -= cly;
            }
            if (e.changedTouches.length >= 2) {
                secondTouch = true;
                secondPoint = new facecat.FCPoint(e.changedTouches[1].x, e.changedTouches[1].y);
                secondPoint.x /= paint.scaleFactorX;
                secondPoint.y /= paint.scaleFactorY;
                secondPoint.x -= clx;
                secondPoint.y -= cly;
            }
            paint.firstTouch = firstTouch;
            paint.touchFirstPoint = firstPoint;
            paint.secondTouch = secondTouch;
            paint.touchSecondPoint = secondPoint;
            if(paint.app){
                if(paint.focusedView && paint.focusedView.viewType == "textbox"){
                    showTextBox(paint.focusedView, lastFocusedView);
                }else{
                    hideTextBox(paint.focusedView);
                }
            }
            if(callBack){
                callBack(paint.touchDownView, firstTouch, firstPoint, secondTouch, secondPoint);
            }else{
                facecat.onTouchBeginDefault(paint.touchDownView, firstTouch, firstPoint, secondTouch, secondPoint);
            }
            if(paint.touchDownView.allowResize){
                paint.touchDownView.resizePoint = facecat.getResizeState(paint.touchDownView, firstPoint);
                if(paint.touchDownView.resizePoint != -1){
                    paint.touchDownView.startRect = new facecat.FCRect(paint.touchDownView.location.x, paint.touchDownView.location.y, paint.touchDownView.location.x + paint.touchDownView.size.cx,
                        paint.touchDownView.location.y + paint.touchDownView.size.cy);
                }
            }
        }
    }
};

/*
* 触摸移动
*/
let handleTouchMove =  function(paint, e, callBack){
    if (paint.isMobile) {
		if (paint.touchDownView) {
            let mp = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
            mp.x /= paint.scaleFactorX;
            mp.y /= paint.scaleFactorY;
            let firstTouch = paint.firstTouch, secondTouch = paint.secondTouch;
            let firstPoint = paint.touchFirstPoint;
            let secondPoint = paint.touchSecondPoint;
            let pParent = facecat.findPreviewsEventParent(paint.focusedView);
				if (pParent && pParent.allowDragScroll && (pParent.viewType == "div" || pParent.viewType == "layout" || pParent.viewType == "menu")) {
					paint.touchMoveView = pParent;
					paint.focusedView = pParent;
					paint.touchDownView = pParent;
					let clx = facecat.clientX(paint.touchDownView);
                    let cly = facecat.clientY(paint.touchDownView);
					if (e.changedTouches.length >= 1) {
                        firstTouch = true;
                        firstPoint = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
                        firstPoint.x /= paint.scaleFactorX;
                        firstPoint.y /= paint.scaleFactorY;
                        firstPoint.x -= clx;
                        firstPoint.y -= cly;
                    }
                    if (e.changedTouches.length >= 2) {
                        secondTouch = true;
                        secondPoint = new facecat.FCPoint(e.changedTouches[1].x, e.changedTouches[1].y);
                        secondPoint.x /= paint.scaleFactorX;
                        secondPoint.y /= paint.scaleFactorY;
                        secondPoint.x -= clx;
                        secondPoint.y -= cly;
                    }
                    if(secondTouch && e.changedTouches.length <= 1){
                        return;
                    }
					facecat.touchDownDiv(pParent, paint.firstTouch, paint.touchFirstPoint, paint.secondTouch, paint.touchSecondPoint, 1);
					facecat.touchMoveDiv(pParent, paint.firstTouch, paint.touchFirstPoint, paint.secondTouch, paint.touchSecondPoint);
					facecat.invalidateView(pParent);
			}else{
                let clx = facecat.clientX(paint.touchDownView);
                let cly = facecat.clientY(paint.touchDownView);
                if (e.changedTouches.length >= 1) {
                    firstTouch = true;
                    firstPoint = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
                    firstPoint.x /= paint.scaleFactorX;
                    firstPoint.y /= paint.scaleFactorY;
                    firstPoint.x -= clx;
                    firstPoint.y -= cly;
                }
                if (e.changedTouches.length >= 2) {
                    secondTouch = true;
                    secondPoint = new facecat.FCPoint(e.changedTouches[1].x, e.changedTouches[1].y);
                    secondPoint.x /= paint.scaleFactorX;
                    secondPoint.y /= paint.scaleFactorY;
                    secondPoint.x -= clx;
                    secondPoint.y -= cly;
                }
                if(secondTouch && e.changedTouches.length <= 1){
                    return;
                }
                paint.firstTouch = firstTouch;
                paint.touchFirstPoint = firstPoint;
                paint.secondTouch = secondTouch;
                paint.touchSecondPoint = secondPoint;
                if(callBack){
                    callBack(paint.touchDownView, firstTouch, firstPoint, secondTouch, secondPoint);
                }else{
                    facecat.onTouchMoveDefault(paint.touchDownView, firstTouch, firstPoint, secondTouch, secondPoint);
                }		
                if(paint.touchDownView.resizePoint != -1){
					let newBounds = new facecat.FCRect(paint.touchDownView.startRect.left, paint.touchDownView.startRect.top, paint.touchDownView.startRect.right, paint.touchDownView.startRect.bottom);
					facecat.windowResize(newBounds, paint.touchDownView.resizePoint, mp, paint.touchDownPoint);
					paint.touchDownView.location = new facecat.FCPoint(newBounds.left, newBounds.top);
					paint.touchDownView.size = new facecat.FCSize(newBounds.right - newBounds.left, newBounds.bottom - newBounds.top);
					if (paint.touchDownView.parent) {
						if (paint.touchDownView.parent.paint) {
							facecat.invalidateView(paint.touchDownView.parent);
						}
					} else {
						if (paint) {
							facecat.invalidate(paint);
						}
					}
				}
                else if (paint.touchDownView.allowDrag) {
                    if (Math.abs(mp.x - paint.touchDownPoint.x) > 5 || Math.abs(mp.y - paint.touchDownPoint.y) > 5) {
                        paint.dragBeginRect = new facecat.FCRect(paint.touchDownView.location.x, paint.touchDownView.location.y,
                        paint.touchDownView.location.x + paint.touchDownView.size.cx,
                        paint.touchDownView.location.y + paint.touchDownView.size.cy);
                        paint.dragBeginPoint = new facecat.FCPoint(paint.touchDownPoint.x, paint.touchDownPoint.y);
                        paint.draggingView = paint.touchDownView;
                        paint.touchDownView = null;
                    }
                }else{

                }
            }
		}
		else if (paint.draggingView != null) {
            let mp2 = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
            mp2.x /= paint.scaleFactorX;
            mp2.y /= paint.scaleFactorY;
			paint.touchDownPoint = new facecat.FCPoint(mp2.x, mp2.y);
			let offsetX = mp2.x - paint.dragBeginPoint.x;
			let offsetY = mp2.y - paint.dragBeginPoint.y;
			let newBounds = new facecat.FCRect(paint.dragBeginRect.left + offsetX, paint.dragBeginRect.top + offsetY,
                paint.dragBeginRect.right + offsetX, paint.dragBeginRect.bottom + offsetY);
			paint.draggingView.location = new facecat.FCPoint(newBounds.left, newBounds.top);
			if (paint.draggingView.parent && paint.draggingView.parent.viewType == "split") {
				facecat.resetSplitLayoutDiv(paint.draggingView.parent);
				if (paint.onUpdateView) {
					paint.onUpdateView(paint.draggingView.parent.views);
				} else {
					facecat.updateViewDefault(paint.draggingView.parent.views);
				}
			}
			if (paint.draggingView.parent) {
				if (paint.draggingView.parent.paint) {
					facecat.invalidateView(paint.draggingView.parent);
				}
			} else {
				if (paint) {
					facecat.invalidate(paint);
				}
			}
		}
	}
};

/*
* 触摸结束
*/
let handleTouchEnd = function(paint, e, callBack, clickCallBack){
    if (paint.isMobile) {
		if (paint.touchDownView) {
			let touchDownView = paint.touchDownView;
            let mp = new facecat.FCPoint(e.changedTouches[0].x, e.changedTouches[0].y);
            mp.x /= paint.scaleFactorX;
            mp.y /= paint.scaleFactorY;
			let cmp = new facecat.FCPoint(mp.x - facecat.clientX(paint.touchDownView), mp.y - facecat.clientY(paint.touchDownView));
			let view = facecat.findView(mp, paint.views);
			let clicks = 1;
			if(paint.isDoubleClick){
				clicks = 2;
			}
			if (view != null && view == paint.touchDownView) {
                if(clickCallBack){
                    facecat.onClickDefault(paint.touchDownView, cmp, 1, clicks, 0);
                }else{
                    clickCallBack(paint.touchDownView, cmp, 1, clicks, 0);
                }
            }
            paint.touchDownView.resizePoint = -1;
            paint.touchDownView = null;
            if(callBack){
                facecat.onTouchEndDefault(touchDownView, paint.firstTouch, cmp, paint.secondTouch, paint.touchSecondPoint);
            }else{
                callBack(touchDownView, paint.firstTouch, cmp, paint.secondTouch, paint.touchSecondPoint);
            }
		}
		paint.draggingView = null;
	}
};

/*
* 缩放图形
*/
let scaleCanvas = function(paint, cres){
    paint.canvas = cres[0].node;
    const dpr = wx.getSystemInfoSync().pixelRatio;
    paint.canvas.width = cres[0].width * dpr;
    paint.canvas.height = cres[0].height * dpr;
    const ctx = paint.canvas.getContext('2d');
    paint.ratio = dpr;
    ctx.scale(dpr, dpr)
    paint.isMobile = true;
    let canvasSize = new facecat.FCSize((paint.canvas.width / paint.ratio / paint.scaleFactorX), (paint.canvas.height / paint.ratio / paint.scaleFactorY));
    paint.size = canvasSize;
};

module.exports = {
    setAttributeWeChat,
    renderFaceCatWeChat,
    readXmlNodeWeChat,
    handleTouchStart,
    handleTouchMove,
    handleTouchEnd,
    scaleCanvas
}